(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      docType = 4,
      newsId = urlParams['newsId'],
      id = urlParams['id'];
    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen" id="j-screen-'+pageName+id+'"></div>');
    $('body').append(pageScreen);

    specialShow();

    // 获取主题列表
    function specialShow(){
      var _data ={};
      if(docType == '1'){//
        _data = {"id": id, "newsId": newsId};
      }else{
        _data = {"id": id};
      }
      sugarcane.ajaxApi({
        "url": "/special/detail",
        "data": _data,
        "callback": function(res){
          if (res['code'] == 0) {
            if (res['data']) {
              //document.title = res.data.title;
              sugarcane.twoShare({
                "id": res.data.id,
                "title": res.data.title,
                "desc": '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',
                "imgUrl": res.data.shareImage,
                "link": res.data.shareUrl,
                "docType": res.data.docType
              });
              // themehead
              var themeheadObj = newGrapes["themehead"].init({"imgurl":res['data']['coverImage'],"title":res['data']['title']});

              themeheadObj.find('.news-picture img').width('100%');
              themeheadObj.find('.news-sub').remove();
              pageScreen.append(themeheadObj);

              var specialColumn = res['data']['specialColumn'];
              sugarcane.module.load('listitem', function(listitemFn){
                // 遍历栏目
                $.each(specialColumn, function(){
                  var themetitleObj = newGrapes["themetitle"].init({"icon":"","title":this['title'],"more":"更多 &gt;"});
                  pageScreen.append(themetitleObj);
                  
                  // 点击更多
                  var _moreHref = 'specialcolumn.html?columnid='+this['id']+'&columnname='+this['title']+ '&themeId='+ id+ '&docType='+ res.data.docType;
                  themetitleObj.find('.more').on(tap, function(){
                    pageScreen.data('scrolltop', $(window).scrollTop());
                    sugarcane.route.location({
                      'href': _moreHref
                    });
                  });
                  
                  $.each(this['news'], function(){
                    var _obj = listitemFn(this);
                    if (_obj) {
                      pageScreen.append(_obj);
                      // 点击新闻详情
                      var _detailHref = 'detail.html?id='+this['id'];
                      _obj.on(tap, function(){
                        pageScreen.data('scrolltop', $(window).scrollTop());
                        sugarcane.route.location({
                          'href': _detailHref
                        });
                      });
                    }
                  });
                });
              });
              if(res.data.docType){
                docType = res.data.docType;
              }
              // shareBanner();
              if(docType == '1'){//入口专题分享
                sugarcane['shareBanner']({
                  'id': newsId,
                  'docType': docType,
                  'containObj': pageScreen 
                });
              }else if(docType == '4'){//专题分享
                sugarcane['shareBanner']({
                  'id': id,
                  'docType': docType,
                  'containObj': pageScreen 
                });
              }
              

            }
          } else {
            showerrorView();
            peach.hiddenAppbar()
            // alert(res['message']);
          }
        }
      });
    }
    
    // function shareBanner(){
    //   if(sugarcane['app']['isShare'] && !pageScreen.find('.app-banner').length ){
    //     sugarcane.module.load('banner', function(listitemFn){
    //       var bannerObj = listitemFn({'id': id, 'docType': docType});
    //       pageScreen.append(bannerObj);
    //       var _height = $(bannerObj).css('height');
    //       pageScreen.css('margin-bottom',_height);
    //     });

    //   }
    // }

    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }
    
  };

  

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('special'));
